<?php

namespace app\store\controller;

use controller\BasicAdmin;
use think\Db;

class User extends BasicAdmin
{
    public $table = 'StoreMember';

    public function index()
    {
        $this->title = '用户列表';
        $db = Db::name($this->table);
        $p_db = Db::name($this->table);
        $get = $this->request->get();
        foreach(['nickname', 'mobile', 'ref', 'sid'] as $field) {
            if (isset($get[$field]) && $get[$field] !== '') {
                $db->where($field, 'like', "%{$get[$field]}%");
                $p_db->where($field, 'like', "%{$get[$field]}%");
            }
        }
        if (isset($get['user_type']) && $get['user_type'] !== ''){
            $db->where('user_type',$get['user_type']);
            $p_db->where('user_type', 'like', "%{$get['user_type']}%");
        }
        if (isset($get['date']) && $get['date'] !== '') {
            list($start, $end) = explode(' - ', $get['date']);
            $db->whereBetween('create_time', [strtotime("{$start} 00:00:00"), strtotime("{$end} 23:59:59")]);
            $p_db->whereBetween('create_time', [strtotime("{$start} 00:00:00"), strtotime("{$end} 23:59:59")]);
        }
        $sort_item = 'id desc';
        $field = isset($get['field']) ? trim($get['field']) : '';
        $sort = isset($get['sort']) ? trim($get['sort']) : 'desc';

        $count = $db->sum('money_sum');
        $sum_p = $p_db->where('money_sum', '<>', 0)->count();
        $this->assign('count', $count);
        $this->assign('sum_p', $sum_p);

        if($field) {
            $sort_item = $field.' '.$sort;
        }
        return parent::_list($db->order($sort_item));
    }
}
